package ca.unbc.cpsc672.plot;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Canvas;

public class PlotCanvas {
	private int xPixelLength;
	private int yPixelLength;
	private GC gc;
	private double maxVal;
	private Image image;

	public PlotCanvas(Canvas canvas, double maxVal) {
		Point sz = canvas.getSize();
		xPixelLength = sz.x - 100;
		yPixelLength = sz.y - 60;
		this.maxVal = maxVal;
		image = new Image(canvas.getDisplay(), sz.x, sz.y);
		gc = new GC(image);
		drawAxis();
		gc.setLineWidth(1);
	}

	public GC getGC() {
		return gc;
	}

	public int calcPixelX(double v) {
		return (int) (v / maxVal * xPixelLength) + 30;
	}

	public int calcPixelY(double v) {
		return (int) (yPixelLength * (1 - v) + 40);
	}

	public double getPointPixcelRatio() {
		return maxVal / xPixelLength;
	}
	
	public double getMaxVal() {
		return maxVal;
	}

	public void setFGColor(int c) {
		gc.setForeground(gc.getDevice().getSystemColor(c));
	}

	private void drawAxis() {
		setFGColor(SWT.COLOR_BLACK);
		gc.setLineWidth(4);
		gc.drawLine(calcPixelX(0) - 5, calcPixelY(0), calcPixelX(maxVal),
				calcPixelY(0));
		gc.drawLine(calcPixelX(0), calcPixelY(0), calcPixelX(0),
				calcPixelY(1.1));
	}

	public Image getImage() {
		return image;
	}
}
